{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 align=\"center\"> PyQt4: Making toggle radio button which opens different combo boxes on the same window </h1>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Original Question: http://stackoverflow.com/questions/38210976/pyqt4-making-toggle-radio-button-which-opens-different-combo-boxes-on-the-same"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1) Allow the user to toggle a radio button with which analysis the individual wants to execute (2 options)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2) Depending on which radio button is toggled, add certain comboboxes below the radio buttons."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3) Create dependency between comboboxes."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4) When the 'Reset Parameters' pushbutton is pushed, all of the parameters are reset."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2 align=\"center\"></h2> <p></p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from PyQt4 import QtGui\n",
    "import os, sys\n",
    "import pandas as pd\n",
    "import pandas_datareader.data as web\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas\n",
    "from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "from PyQt4 import QtGui, QtCore\n",
    "from PyQt4.QtGui import *\n",
    "from PyQt4.QtCore import *\n",
    "import sys\n",
    "\n",
    "class TabManager(QtGui.QTabWidget):\n",
    "    \n",
    "    def __init__(self):\n",
    "        super(TabManager, self).__init__()\n",
    "        self.initUI()\n",
    "        \n",
    "    def initUI(self):\n",
    "        self.setGeometry(1000,1000, 1000, 1000)\n",
    "        self.setWindowTitle('Revision on Plots, Tables and File Browser')     \n",
    "        \n",
    "        #Grid Layout for tab1\n",
    "        self.grid = QtGui.QGridLayout()\n",
    "        \n",
    "        # Create tabs\n",
    "        tab1 = QtGui.QWidget() # extra tabs in case you need later\n",
    "        tab2 = QtGui.QWidget()\n",
    "        tab3 = QtGui.QWidget()\n",
    "        tab4 = QtGui.QWidget()\n",
    "\n",
    "        # Resize width and height\n",
    "        self.resize(1000, 1000)\n",
    "        self.setLayout(self.grid)\n",
    "         \n",
    "\n",
    "        # Add tab1 (ProQinase Cancer Cell Lines)\n",
    "        self.addTab(tab1,\"ProQinase Cancer Cell Lines\")\n",
    "        \n",
    "        self.button_group = QButtonGroup()\n",
    "        # Buttons\n",
    "        self.explore_button = QRadioButton('Explore Individual Cell Lines')\n",
    "        self.correlation_button = QRadioButton('Cell Line Correlation')\n",
    "\n",
    "        #use button groups\n",
    "        # http://stackoverflow.com/questions/17402452/how-to-get-the-checked-radiobutton-from-a-groupbox-in-pyqt\n",
    "        \n",
    "        self.button_group.addButton(self.explore_button, 0) # add button at index 0 \n",
    "        self.button_group.addButton(self.correlation_button, 1)\n",
    "        \n",
    "        # Connect each radio button to a method to run when it's clicked\n",
    "        self.connect(self.explore_button, SIGNAL(\"clicked()\"), self.radio_button_clicked)\n",
    "        self.connect(self.correlation_button, SIGNAL(\"clicked()\"), self.radio_button_clicked)\n",
    "        \n",
    "        # Set layout of first tab\n",
    "        # button group above is just a way to check if either button is pressed\n",
    "        self.grid.addWidget(self.explore_button, 1,0)\n",
    "        self.grid.addWidget(self.correlation_button, 1,1)\n",
    "        tab1.setGeometry(1000,1000, 1000, 1000)\n",
    "        tab1.setLayout(self.grid) \n",
    "        self.addTab(tab2,\"Tab 2\")\n",
    "        self.addTab(tab3,\"Tab 3\")\n",
    "        self.addTab(tab4,\"Tab 4\") \n",
    "        self.show()\n",
    "        \n",
    "    def radio_button_clicked(self):\n",
    "        print(self.button_group.checkedId())\n",
    "        \n",
    "        # self.explore_button is index 0\n",
    "        if self.button_group.checkedId() == 0: \n",
    "            self.dropdown1 = QtGui.QComboBox(self)\n",
    "            self.parameters = ['x', 'y', 'z']\n",
    "            self.dropdown1.addItems(self.parameters)\n",
    "            self.grid.addWidget(self.dropdown1, 2,0)\n",
    "\n",
    "        elif self.button_group.checkedId() == 1: \n",
    "            self.dropdown2 = QtGui.QComboBox(self)\n",
    "            self.parameters2 = ['CPQ #', 'CCLE #']\n",
    "            self.dropdown2.addItems(self.parameters2)\n",
    "            self.grid.addWidget(self.dropdown2, 2,1)\n",
    "            self.grid.removeWidget(self.dropdown1)\n",
    "\n",
    "            \n",
    "        #self.grid.addWidget(self.explore_button, 1,0)\n",
    "        #print(self.button_group.checkedButton().text())\n",
    "\n",
    "\n",
    "    \n",
    "        \n",
    "def main():\n",
    "    app = QtGui.QApplication(sys.argv)\n",
    "    w = TabManager()\n",
    "    app.exec_()\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
